(function(){
	var cal = $('#cal-layer,#cal_layer'),
		list = $('div.cal-year-item', cal),
		year = $('b', cal),
		currentYear   = null,
		selectedMonth = null,
		selectedYear  = null,
		selectedYearIndex = 0;


	cal.on('update', function(){
		if(selectedYear)year.html(selectedYear.html());//update ui
		$(this).data('value', [
			selectedYear ? parseInt(selectedYear.html()) : '',
			selectedMonth ? parseInt(selectedMonth.html()) : ''
		]);
	});

	$('button.minor,i.close', cal).click(function(){//close btn
		$(this).parent().parent().trigger('close');
		return false;
	});

	$.each(list.children(), function(i){//search selected year
		var o = $(this);
		o.data('I', i);
		if(o.hasClass('hover')){
			selectedYear = currentYear = o;
			selectedYearIndex = i;
		}
	});

	list.on('click', 'span', function(){//year list
		var o = $(this);
		if(!(o.hasClass('hover')) && selectedYear != o){
			if(selectedYear !== null){
				selectedYear.removeClass('hover');
			}
			(selectedYear = o).addClass('hover');
			selectedYearIndex = o.data('I');
			cal.trigger('update');
			list.addClass('hide');
		}
		return false;
	});

	cal.on('click', 'span', function(){//prev this next year
		switch(this.className){
			case 'prev':
				if(selectedYearIndex > 0){
					selectedYearIndex--;
					selectedYear = selectedYear.removeClass('hover').prev().addClass('hover');
				}
				break;
			case 'next':
				if(selectedYearIndex < list.children().length-1){
					selectedYearIndex++;
					selectedYear = selectedYear.removeClass('hover').next().addClass('hover');
				}
				break;
			case 'this':
				if(currentYear){
					selectedYear.removeClass('hover');
					selectedYear = currentYear.addClass('hover');
					selectedYearIndex = currentYear.data('I');
				}
				break;
			default:
				list.toggleClass('hide');
				break;
		}
		cal.trigger('update');
	});

	cal.on('click', 'td', function(){//month
		var o = $(this);
		if(!!o.attr('colspan') || (selectedMonth === o))return false;
		if(selectedMonth)selectedMonth.removeClass('cur');
		selectedMonth = o;
		selectedMonth.addClass('cur');
		cal.trigger('update');
	});

	cal.trigger('update');
})();

/*
var w$ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
  create: function() {
    return function() {
	  this.initialize.apply(this, arguments);
    }
  }
}

var Extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}


var Calendar = Class.create();
Calendar.prototype = {
  initialize: function(container, options) {
  
	this.Container = w$(container);//瀹瑰??(table缁????)

	this.Days = [];//??ユ??瀵硅薄???琛?
	
	this.SetOptions(options);
	
	this.Year = this.options.Year || new Date().getFullYear();
	this.Month = this.options.Month || new Date().getMonth() + 1;
	this.Today = this.options.Today || new Date().getDate();
	this.Count = this.options.Count;
	this.Edit = this.options.Edit;
	this.Remain = this.options.Remain;
	this.SelectDay = this.options.SelectDay ? new Date(this.options.SelectDay) : null;
	this.onSelectDay = this.options.onSelectDay;
	this.onToday = this.options.onToday;
	this.onExpired = this.options.onExpired;
	this.onFinish = this.options.onFinish;
	this.Draw();
	if(this.Count){
		var cale = new Calendar("idCalendar1", {
			Year:			this.Year,
			Month:			parseInt(this.Month)+1,
			Today:			this.Today,
			Edit:			true,
			onFinish: function(){
				$("#idCalendarYear1").text(this.Year); $("#idCalendarMonth1").text(this.Month);
			}
		});
		var cale = new Calendar("idCalendar2", {
			Year:			this.Year,
			Month:			parseInt(this.Month)+2,
			Today:			this.Today,
			Edit:			true,
			onFinish: function(){
				$("#idCalendarYear2").text(this.Year); $("#idCalendarMonth2").text(this.Month);
			}
		});
		if(0<this.Today){
			var cale = new Calendar("idCalendar3", {
				Year:			this.Year,
				Month:			parseInt(this.Month)+3,
				Today:			this.Today,
				Edit:			true,
				Remain:         true,
				onFinish: function(){
					$("#idCalendarYear3").text(this.Year); $("#idCalendarMonth3").text(this.Month);
				}
			})
		}
	}
  },
  //璁剧疆榛?璁ゅ?????
  SetOptions: function(options) {
	this.options = {//榛?璁ゅ??
		Year:			0,//??剧ず骞?
		Month:			0,//??剧ず???
		Today:			0,//??剧ず???
		Count:			0,//??剧ず??诲ぉ???
		Remain:         false,//琛ヨ冻涓?涓????澶╂??
		Edit:			false,//??????缂?杈?
		SelectDay:		null,//?????╂?ユ??
		onSelectDay:	function(){},//??ㄩ????╂?ユ??瑙????
		onToday:		function(o){
			o.className = "onToday";
		},//??ㄥ??澶╂?ユ??瑙????
		onFinish:		function(){},//??ュ????诲?????瑙????
		onExpired:		function(o){
			o.className = "expired";
		}
	};
	Extend(this.options, options || {});
  },
  //??绘?ュ??
  Draw: function() {
	//??ㄦ?ヤ??瀛???ユ?????琛?
	var arr = [];
	//??ㄥ?????绗?涓?澶╁?ㄤ????ㄤ腑?????ユ????间??涓哄?????绂荤??涓?澶╃??澶╂??
	for(var i = 1, firstDay = new Date(this.Year, this.Month - 1, 1).getDay(); i <= firstDay; i++){ arr.push(0); }
	//??ㄥ???????????涓?澶╁?ㄤ??涓????涓??????ユ????间??涓哄????????澶╂??
	for(var i = 1, monthDay = new Date(this.Year, this.Month, 0).getDate(); i <= monthDay; i++){ arr.push(i); }
	//娓?绌哄????ョ????ユ??瀵硅薄???琛?
	this.Days = [];
	//?????ユ?ユ??
	var frag = document.createDocumentFragment();
	if(12<this.Month){
		this.Year = parseInt(this.Year) + 1;
	}
	if(this.Month == 13){
		this.Month = parseInt(this.Month)-12;
	}
	else if(this.Month == 14){
		this.Month = parseInt(this.Month)-12;
	}
	else if(this.Month == 15){
		this.Month = parseInt(this.Month)-12;
	}
	while(arr.length){
		//姣?涓????????????ヤ??涓?tr
		var row = document.createElement("tr");
		//姣?涓??????????7澶?
		
		for(var i = 1; i <= 7; i++){
			var cell = document.createElement("td"); cell.innerHTML = "&nbsp;";
			var div = document.createElement("div"); 
			if(arr.length){
				var d = arr.shift();
				if(d){
					cell.innerHTML = d;
					this.Days[d] = cell;
					var input = "<input type='text' name='"+this.Year+'-'+this.o2f(this.Month)+'-'+ this.o2f(d) +"' value=''>";
					if(this.Edit){
						cell.appendChild(div);
						div.innerHTML=input;
					}
					var on = new Date(this.Year, this.Month - 1, d);
					//??ゆ????????浠????
					
					if(this.Today){
						this.IsSame(on, new Date(this.Year, parseInt(this.Month) - 1,this.Today)) && this.onToday(cell);
						if(this.Count){
							for(var j=0; j<this.Today; j++){
								this.IsSame(on, new Date(this.Year,parseInt(this.Month) - 1,j)) && this.onExpired(cell);
							}
						}
						if(this.Remain){
							var curDay = new Date(this.Year, this.Month,0).getDate();
							var k = this.Today;
							for(k; k<=curDay;k++){
								this.IsSame(on, new Date(this.Year, parseInt(this.Month)-1,k)) && this.onExpired(cell);
							}
						}
					}
					else{
						this.IsSame(on, new Date()) && this.onToday(cell);
					}
					//??ゆ?????????????╂?ユ??
					this.SelectDay && this.IsSame(on, this.SelectDay) && this.onSelectDay(cell);
				}
			}
			row.appendChild(cell);
		}
		frag.appendChild(row);
	}
	//???娓?绌哄??瀹瑰????????(ie???table涓???界??innerHTML)
	while(this.Container.hasChildNodes()){ this.Container.removeChild(this.Container.firstChild); }
	this.Container.appendChild(frag);
	//??????绋?搴?
	this.onFinish();
	},
	//??ゆ???????????涓????
	IsSame: function(d1, d2){
		return (d1.getFullYear() == d2.getFullYear() && d1.getMonth() == d2.getMonth() && d1.getDate() == d2.getDate());
	},
	o2f:function (n){
		return n>9?n:("0"+n);
	}
}
*/
